//
//  CZOAuthViewController.m
//  CZOC微博01
//
//  Created by 1 on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "CZOAuthViewController.h"
#import  "AFNetworking.h"
//#import  "CZFeatherViewController.h"
//#import "CZTabbarController.h"
#import "CZAccount.h"
#import  "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"
#import "CZAccountTool.h"
#import "UIWindow+CZExtenWindow.h"
@interface CZOAuthViewController ()<UIWebViewDelegate>

@end

@implementation CZOAuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIWebView *webView=[[UIWebView alloc]init];
    
//    https://api.weibo.com/oauth2/authorize
    
    webView.frame=self.view.bounds;
    webView.delegate=self;
    [self.view addSubview:webView];
    NSURL *url=[NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3189333321&redirect_uri=http://www.baidu.com/"];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *url=request.URL.absoluteString;
    NSRange range=[url rangeOfString:@"code="];
     if(range.length>0)
     {
         NSUInteger formIndex=range.length+range.location;
         NSString *code=[url substringFromIndex:formIndex];
//         NSLog(@"%@",code);
         [self getAccseeTokenWithCode:code];
         //禁止回调地址
         return NO;
     }
    
    return YES;
    
    
}
//再次发动一个请求利用code
- (void)getAccseeTokenWithCode:(NSString *)code
{
    
    AFHTTPRequestOperationManager *request=[AFHTTPRequestOperationManager manager];
    //设置它的返回的response
//    request.responseSerializer=[AFJSONResponseSerializer serializer];
    //用于文件的传输
   
    
    NSMutableDictionary *dic=[NSMutableDictionary  dictionary];
     dic[@"client_id"]=@"3189333321";
    dic[@"client_secret"]=@"2fd248ee1b61c8ce47723944a267bed5";
    dic[@"grant_type"]=@"authorization_code";
    dic[@"code"]=code;
    dic[@"redirect_uri"]=@"http://www.baidu.com/";
    [request POST:@"https://api.weibo.com/oauth2/access_token" parameters:dic success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
        
        [MBProgressHUD hideHUD];
        
//        NSLog(@"%@",responseObject);
//        NSString *str=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//        NSString *strPat=[str stringByAppendingPathComponent:@"account.data"];
//        
        CZAccount *account=[CZAccount accountWithDic:responseObject];
//
//        [NSKeyedArchiver archiveRootObject:account toFile:strPat];
        
        
//        [responseObject writeToFile:strPat atomically:YES];
        //保存数据
        
        [CZAccountTool saveAccount:account];
        
        
        //判断是否是第一次的登录
        UIWindow *window=[UIApplication sharedApplication].keyWindow;
        [window switchRootViewController];
//        UIWindow *window=[UIApplication sharedApplication].keyWindow;
//        NSString *key=@"CFBundleVersion";
//        
//        NSUserDefaults *userSet=[NSUserDefaults  standardUserDefaults];
//        NSString * bundle=[userSet objectForKey:key];
//        
//        
//        //获取当前的bundle
//        NSString *currentBundle=[NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
//        if([currentBundle isEqualToString:bundle])
//        {
//              CZTabbarController *tabbar=[[CZTabbarController alloc]init];
//               window.rootViewController=tabbar;
//        }else
//        {
//            CZFeatherViewController *newFeather=[[CZFeatherViewController alloc] init];
//             window.rootViewController=newFeather;
//            [[NSUserDefaults standardUserDefaults] setObject:currentBundle forKey:key];
//            
//            
//        }

        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"errot=%@",error);
        [MBProgressHUD hideHUD];
    }];
    
}
/*
 "access_token" = "2.00vlqvSGBSHqTDec316308f70PO_k3";
 "expires_in" = 157679999;
 "remind_in" = 157679999;
 uid = 5776558595;
 */
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUD];
    
    NSLog(@"开始");
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD showMessage:@"开始加载"];
//    MBProgressHUD showHUDAddedTo:<#(UIView *)#> animated:<#(BOOL)#>
    NSLog(@"结束");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [MBProgressHUD hideHUD];
}


@end
